<template>
    <weui-input :list="input_0_list" title="" :show_more="false" :tips="null" agreement_name=""
                more_label=""
                @on-button="on_input_0_button"
                @on-change="on_input_0_change">
        <weui-button slot="button" type="primary"
                     :is_disable="is_button_0_disable"
                     @on-click="on_register_button_click">{{buttonLabel}}
        </weui-button>
    </weui-input>
</template>

<script>
    import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
    import WeuiButton from '../vue2componentSet/src/components/WeuiButton'
    import global from '../lib/global'
    import axios from 'axios'

    export default {
        components: {
            WeuiInput, WeuiButton
        },
        data() {
            return {
                input_0_list: [{
                    label: '手机号码',
                    value: '',
                    type: 'tel'
                }, {
                    label: '密码',
                    value: '',
                    type: 'password',
                    is_hide: true
                }, {
                    label: '返现方式',
                    type: 'select',
                    value: 'wechat',
                    list: [{
                        label: '微信红包',
                        value: 'wechat'
                    }, {
                        label: '支付宝',
                        value: 'alipay'
                    }]
                }, {
                    label: '支付宝账户',
                    value: '',
                    type: 'text',
                    placeholder: '返现到这里',
                    is_hide: true
                }, {
                    label: '验证码',
                    value: '',
                    type: 'tel',
                    button: '获取',
                }, {
                    label: '推荐人',
                    value: '',
                    type: 'tel',
                    placeholder: '可空'
                }],
                buttonLabel: '注册'
            }
        },
        methods: {
            on_input_0_button(index) {
                if (index == 4) {
                    if (this.$store.state.code_countdown != 0)
                        return
                    if (this.input_0_list[0].value.length != 11)
                        return
                    axios.post(global.server + 'getCode.do', {
                        phone: this.input_0_list[0].value
                    }).then(result => {
                        var obj = result.data
                        if (obj.result == 1) {
                            this.$store.commit('code_countdown')
                        }
                    }, result => {
                        console.log(result)
                    })
                }
            },
            on_register_button_click() {
                if (!this.input_0_list[0].value ||
                    !this.input_0_list[4].value) {
                        return
                    }
                var data = {
                    phone: this.input_0_list[0].value,
                    returnWay: this.input_0_list[2].value,
                    code: this.input_0_list[4].value,
                }
                if (this.input_0_list[1].value)
                    data.password = require('md5')(this.input_0_list[1].value)
                if (this.input_0_list[2].value == 'alipay') {
                    if (!this.input_0_list[3].value)
                        return
                    data.alipay = this.input_0_list[3].value
                }
                if (this.input_0_list[5].value)
                    data.promoter = this.input_0_list[5].value
                axios.post(global.server + 'register.do', data).then(result => {
                    var obj = result.data
                    if (obj.result == 1) {
                        this.$store.commit('customer_login', {
                            phone: this.input_0_list[0].value,
                            alipay: obj.alipay,
                            wechatQrcode: obj.wechatQrcode,
                            isPromoter: obj.isPromoter
                        })
                        obj.isAdmin && this.$store.commit('admin_login', {
                            phone: this.input_0_list[0].value
                        })
                        if (require('../lib/browserDetect')().wechat) {
                            location.href = '/返利教程（公众号）.html'
                        } else {
                            this.$router.goback()
                        }
                    } else {
                        this.$store.commit('toast', {
                            type: 'warn',
                            content: obj.msg
                        })
                    }
                })
            },
            on_input_0_change(index) {
                if (this.input_0_list[2].value == 'alipay')
                    this.$set(this.input_0_list[3], 'is_hide', false)
                else
                    this.$set(this.input_0_list[3], 'is_hide', true)
            }
        },
        computed: {
            code_countdown() {
                return this.$store.state.code_countdown
            },
            is_button_0_disable() {
                if (!this.input_0_list[0].value)
                    return true
                if (!this.input_0_list[1].value)
                    if (!require('../lib/browserDetect')().wechat)
                        return true
                if (this.input_0_list[2].value == 'alipay')
                    if (!this.input_0_list[3].value)
                        return true
                if (!this.input_0_list[4].value)
                    return true
                return false
            }
        },
        watch: {
            code_countdown(value) {
                if (value == 0)
                    this.$set(this.input_0_list[4], 'button', '获取')
                else
                    this.$set(this.input_0_list[4], 'button', value)
            },
        },
        mounted() {
            if (require('../lib/browserDetect')().wechat) {
                this.$store.commit('set_title', '绑定')
                this.buttonLabel = '绑定'
            } else {
                this.$store.commit('set_title', '注册')
                this.$set(this.input_0_list[1], 'is_hide', false)
            }

            location.hash.split('?')[1] && location.hash.split('?')[1].split('&').forEach(item => {
                if (item.split('=')[0] == 'promoter')
                    this.input_0_list[5].value = item.split('=')[1]
            })
        }
    }
</script>
